package com.ericsson.rm.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.ericsson.rm.form.InsertResourceCommand;
import com.ericsson.rm.service.ResourceInsertService;
import com.ericsson.rm.util.Utility;
import com.ericsson.rm.validator.ResourceInsertValidator;

import com.ericsson.rm.vo.ResourceInsertVO;

@Controller

public class ResourceInsertController {

  private final static Logger logger = LoggerFactory.getLogger(ResourceInsertController.class);
  private final static String CLASS_NAME = ResourceInsertController.class.getSimpleName();
  private final static String PAGE_INSERT_RESOURCE_FORM = "insertResource";
  
  private ResourceInsertValidator resourceInsertValidator;
  
  @Autowired
  ResourceInsertService resourceInsertService;
  
  
  public ResourceInsertService getResourceInsertService() {
    return resourceInsertService;
  }

  public void setResourceInsertService(ResourceInsertService resourceInsertService) {
    this.resourceInsertService = resourceInsertService;
  }

  @RequestMapping("InsertResource")
  public String initForm(ModelMap model)
  {
    final String METHOD_NAME = CLASS_NAME + ".initForm() ";
    System.out.println("Init method......................................");
    if(logger.isDebugEnabled()){
      logger.debug(METHOD_NAME+"entering...");
    }
    
    InsertResourceCommand insertResourceCmd = new InsertResourceCommand();
    model.addAttribute("InsertResourceCommand",insertResourceCmd);
    return PAGE_INSERT_RESOURCE_FORM;
  }
  
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    final String METHOD_NAME = CLASS_NAME + ".initBinder() ";
    if(logger.isDebugEnabled()){
      logger.debug(METHOD_NAME+"entering...");
    }
    
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
  }
  
  @Autowired
  public ResourceInsertController(ResourceInsertValidator resourceInsertValidator){
    if(logger.isDebugEnabled()){
      logger.debug(CLASS_NAME+" constructor entering...");
    }
    
    this.resourceInsertValidator = resourceInsertValidator;
  }
  
  @RequestMapping(method = RequestMethod.POST)
    public String insertResource(@ModelAttribute("InsertResourceCommand") InsertResourceCommand insertResourceCommand, BindingResult result, SessionStatus status) {
        final String METHOD_NAME = CLASS_NAME + "insertResource() ";
     
        if(logger.isDebugEnabled()){
            logger.debug(METHOD_NAME+"entering...");
            logger.debug(METHOD_NAME+"SearchResourceCommand: "+Utility.displayBean(insertResourceCommand));
        }
       
        //  resourceInsertValidator.validate(insertResourceCommand, result);
    // resourceSearchValidator.validate(searchResourceCommand, result);
           if(result.hasErrors())
           {
             return "fail";
           }
           else
           {
             ResourceInsertVO resourceInsertVO = (ResourceInsertVO)Utility.copyBean(insertResourceCommand, ResourceInsertVO.class);
             if(logger.isDebugEnabled()){
             logger.debug(METHOD_NAME+"ResourceInsertVO: "+Utility.displayBean(resourceInsertVO));
             } 
          
            resourceInsertService.insertResourceIntoTable(resourceInsertVO);
            status.setComplete();
            return "Success" ;
        }
  }
}
